<?php

namespace App\Http\Controllers\Member;

use App\CustomTrait\StatusReturnTrait;
use App\Model\Materiels;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MaterielController extends CommonController
{
  use StatusReturnTrait;

  public function index()
  {
    return view('member.materiel.index');
  }

  /**
   * 创建基础物料
   * @param Materiels $materiels
   * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
   */
  public function create(Materiels $materiels)
  {
    return view('member.materiel.create', compact('materiels'));
  }

  /**
   * 物料基础信息入库
   * @param Request $request
   * @return \Illuminate\Http\JsonResponse
   */
  public function store(Request $request)
  {
    $param = $request->get('param');
    $data = [
      'member_id' => getMemberId(),
      'type' => $param['type'],
      'name' => $param['name'],
      'overt_phone' => $param['overt_phone'],
      'seat_phone' => $param['seat_phone'],
      'min_age' => $param['min_age'],
      'max_age' => $param['max_age'],
      'sex' => $param['sex'],
      'province' => $param['province'],
      'city' => $param['city']
    ];
    empty($param['license']) ?: $data['license'] = $param['license'];
    empty($param['report']) ?: $data['report'] = $param['report'];
    try {
      DB::beginTransaction();
      Materiels::create($data);
      DB::commit();
      return $this->success(route('materiel.index'), '提交成功,等待管理员审核');
    } catch (\Exception $error) {
      return $this->error('error', '提交失败,请联系客服或者管理员');
    }
  }

  /**
   * 编辑页面
   * @param $id
   * @param Request $request
   * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
   */
  public function edit($id)
  {
    $materiels = Materiels::whereId($id)->first();
    return view('member.materiel.edit', compact('materiels'));
  }

  public function update($id, Request $request)
  {
    $param = $request->get('param');
    $data = [
      'member_id' => getMemberId(),
      'type' => $param['type'],
      'name' => $param['name'],
      'overt_phone' => $param['overt_phone'],
      'seat_phone' => $param['seat_phone'],
      'min_age' => $param['min_age'],
      'max_age' => $param['max_age'],
      'sex' => $param['sex'],
      'province' => $param['province'],
      'city' => $param['city'],
      'status' => false
    ];
    empty($param['license']) ?: $data['license'] = $param['license'];
    empty($param['report']) ?: $data['report'] = $param['report'];
    try {
      DB::beginTransaction();
      Materiels::whereId($id)->update($data);
      DB::commit();
      return $this->success(route('materiel.index'), '编辑成功,等待管理员审核');
    } catch (\Exception $error) {
      return $this->error('error', '编辑失败,请联系客服或者管理员');
    }
  }

  /**
   * 获取数据列表
   * @return \Illuminate\Http\JsonResponse
   */
  public function getLayUiTableData(Request $request)
  {
    $filter = $request->all();
    return Materiels::LayUiTableData($filter, getMemberId());
  }
}
